/*
 * File:        stringUtils.h
 * Authors:     Jonathan Kotker (ee122-bw) and Shaan Mulchandani (ee122-bv)
 * Description: This header file contains utility functions to modify strings.
 */

#ifndef _STRINGUTILS_H
#define	_STRINGUTILS_H

#include <string.h>

/*
 * Destructively converts the given string into all lowercase.
 * This is used for command comparison.
 */
void toLower(char *str) {
    int i;
    for (i = 0; str[i] != '\0'; i++)
        str[i] = (char) tolower(str[i]);
};

/*
 * Replaces the newline character in the string, if any,
 * with a null character.
 */
void replaceNewline(char *str) {
    int i;
    for (i = 0; str[i] != '\0'; i++) {
        if (str[i] == '\n') {
            str[i] = '\0';
            break;
        }
    }
};

/*
 * Sets the destination string (`destn') to the substring of
 * the source (`source') from the start position (`startPos')
 * to the end position (`endPos').
 */
void setString(char* destn, char* source, int startPos, int endPos) {
    int i = 0;
    int j;
    for (j = startPos; j < endPos; i++, j++) {
        destn[i] = source[j];
    }
    destn[endPos - startPos] = '\0';
};

#endif	/* _STRINGUTILS_H */
